package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Setmeal;
import cn.itcast.pojo.User;
import cn.itcast.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference(version = "1.0.0")
    private UserService userService;


    /**
     *  //分页查询用户列表
     * @param queryPageBean
     * @return
     */
    @GetMapping("/page")
    public Result findPage(QueryPageBean queryPageBean){

        PageResult pageResult= null;
        try {
            pageResult = this.userService.findPage(queryPageBean);
            //查询用户信息成功
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            //查询用户信息失败
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }

    }

    /**
     * //根据id查询用户信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id")Integer id){
        try {
            User user=this.userService.findById(id);
            if (user!=null){
                //查询用户成功
                return new Result(true,MessageConstant.QUERY_USER_SUCCESS,user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //查询用户信息失败
        return new Result(false,MessageConstant.QUERY_USER_FAIL);
    }

    /**
     * 新增用户信息信息
     *
     * @param
     * @param
     * @return
     */
    @PostMapping
    public Result handleAdd(@RequestBody User user, int[] roleIds) {
        try {
            Boolean flag = this.userService.handleAdd(user,roleIds);
            if (flag){
                return new Result(true, MessageConstant.ADD_USER_SUCCESS, flag);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true, MessageConstant.ADD_USER_FAIL);
    }

    /**
     * 编辑用户
     * @param roleIds 角色id集合
     * @param user 用户信息
     * @return
     */
    @PutMapping
    public Result handleEdit(int[] roleIds, @RequestBody User user){
        try {
            Boolean flag = userService.handleEdit(roleIds, user);
            if (flag){
                return new Result(true, MessageConstant.EDIT_USER_SUCCESS, flag);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_USER_FAIL);
    }

    /**
     * 根据id删除用户数据
     *
     * @param id
     * @return
     */
    @DeleteMapping(path = "/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        try {
            Integer count=this.userService.deleteById(id);
            if (count>0){
                return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return new Result(false, MessageConstant.DELETE_USER_FAIL);
    }


}
